import { bg_image } from "./indoorConfig.js";
import {userCarList,} from "../api/home/index.js"
import {setDefaultCar,delUserCar,addUserCar,} from "../api/myCar/index.js"
export default {
	data() {
		return {
			bg_image,
			list: [],
			origin: '',
			plate: '',
			showPlateInput: false,
			redirect: 0
		}
	},
	onLoad(options) {
		this.getCarList()
		if (options.origin) {
			this.origin = options.origin
		}
		if (options.redirect) {
			this.redirect = options.redirect
		}
	},
	onPullDownRefresh(){//页面下拉刷新
		this.fetchSomething()
		uni.stopPullDownRefresh();
	},
	methods: {
		async fetchSomething(query){
			this.getCarList()
		},
		async getCarList() {
			let res = await userCarList()
			let data = res.data || []
			this.list = res.data
		},
		async handleSelect(item) {
			if (this.origin === 'scheme') {
				const eventChannel = this.getOpenerEventChannel();
				eventChannel.emit('changePlate', {
					plate: item.plateNumber
				});
				uni.navigateBack()
				return false
			}
			let res = await setDefaultCar(item.id)
			uni.navigateBack({
				url: '/pages/indoor/home'
			})
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.emit('changePlate', {
				plate: item.plateNumber
			});

		},
		handleDelete(data) {
			uni.showModal({
				title: '温馨提示',
				content: `确定删除${data.plateNumber}吗`,
				success: async (res) => {
					if (res.confirm) {
						//删除车牌
						let res = await delUserCar(data.id)
						this.getCarList()
					}

				}
			})
		},
		addCar() {
			this.showPlateInput = true
		},
		handleClose() {
			this.showPlateInput = false
		},
		async handleExport(plate) {
			this.showPlateInput = false
			let res = await addUserCar(plate)
			if (this.redirect === '1') {
				uni.navigateBack({
					delta: 1
				});
				return false
			}
			this.getCarList()
		}
	}
}